/*
 * jQuery CheeZlide plugin 1.0
 *
 * http://www.rvi-media.de/
 *
 * Copyright 2011, Ronald Vilbrandt <info@rvi-media.de>
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * MIKE: Included a minor change to provide for changing of the slide show title on 
 * the calvo observatory page (search for ssTitle to find it).
 */
(function($){
    
    $.fn.cheezlide = function(settings) {

            settings = $.extend({
	                                showSpeed:                  400,
	                                hideSpeed:                  1500,
	                                slideshowTime:              5000,

                                    switchpanel:                true,

	                                provider:                   null,

                                    imageClass:                 "cheezlide_image",
                                    switchpanelContainerClass:  "cheezlide_switchpanel_container",
                                    switchpanelClass:           "cheezlide_switchpanel",

                                    panelOpacity:               0.7

	                            }, settings || {});

            return this.each(function() {

                var setSwitchToActive = function($elem) {
                    // Remove the "active" state from current active navigation element
                    $("." + settings.switchpanelClass + " a", $target).removeClass("active").parents("li").css("opacity", settings.panelOpacity);

                    // Mark the clicked one as active
                    $elem.addClass("active").parents("li").css("opacity", 1);

                    return true;
                };

                var showImage = function($elem) {

                    // Find current visible image
                    var $visible = $("." + settings.imageClass + ":visible", $target);

                    // Fade out visible image
                    hideImage($visible);

                    // Fade in image matching to href (need a small timeout)
                    $elem.fadeIn(settings.showSpeed,function(){changeSSTitle($elem)});

                    // Chrome's too fast :D
                    window.setTimeout(function() {
                                            // Correct position if needed
                                            var itemHeight      = $elem.height();
                                            var itemWidth       = $elem.width();
                                            var targetHeight    = $target.height();
                                            var targetWidth     = $target.width();

                                            if (itemHeight < targetHeight) {
                                                var top = (targetHeight - itemHeight) / 2;
                                                $elem.css("top", top);
                                            }

                                            if (itemWidth < targetWidth) {
                                                var left = (targetWidth - itemWidth) / 2;
                                                $elem.css("left", left);
                                            }
                                        }, 500);
                                        //}, 0 > settings.showSpeed - 75 ? 75 : settings.showSpeed - 75);
                };

                var hideImage = function($elem) {
                    $elem.fadeOut(settings.hideSpeed);
                };

                var slideshow = function() {

                    var $switches = $("." + settings.switchpanelClass + " a", $target);

                    // Find current visible image
                    var $active = $("." + settings.switchpanelClass + " a.active", $target);

                    var indexActive = $switches.index($active);

                    var indexNext = 0;

                    // If there's an image left in loop, change next index to it
                    if (indexActive + 1 < $switches.length) {
                        indexNext = indexActive + 1;
                    }

                    setSwitchToActive($("." + settings.switchpanelClass + " a:eq(" + indexNext + ")", $target));
                    showImage($("." + settings.imageClass + ":eq(" + indexNext + ")", $target));
                }


                var slideshowInterval = null;

                var startSlideshow = function() {
                    slideshowInterval = window.setInterval(slideshow, settings.slideshowTime);
                };

                var stopSlideshow = function() {
                    window.clearInterval(slideshowInterval);
                };

                // Find box
                var $target = $(this);

                // Create nav bar
                var $switchpanel = $("<ul />").addClass(settings.switchpanelClass);

                var $switchpanelContainer = jQuery("<div />").addClass(settings.switchpanelContainerClass);
                $switchpanelContainer.append($switchpanel);

                // Append nav bar to box
                $target.append($switchpanelContainer);

                if (false === settings.switchpanel) {
                    $switchpanel.hide();
                }

                $(settings.provider).each(function(index) {

                    // Clone source image
                    var $item = $(this).clone().addClass(settings.imageClass).css("z-index", index);
                    
                    var $itemLink = $(this).parents("a");

                    $item.click(function() {
                       location.href = $itemLink.attr("href");
                    });

                    // Create navigation links
                    var $switch = $("<a />").attr("href", $item.attr("src")).text($item.attr("title"));

                    // Copy title from alt
                    $item.attr("title", $item.attr("alt"));

                    // Hover events for navigation item
                    $switch.hover(function() {
                                                // Bind to parent <li> instead <a> because of IE8 compatibility!
                                                $(this).parents("li").addClass("hover");
                                                stopSlideshow();
                                            },
                                  function() {
                                                if (false === $(this).hasClass("active")) {
                                                    $(this).parents("li").css("opacity", settings.panelOpacity);
                                                }
                                                $(this).parents("li").removeClass("hover");
                                                startSlideshow();
                                            });

                    // Hover events for image
                    $item.hover(function() {
                                                stopSlideshow();
                                            },
                                  function() {
                                                startSlideshow();
                                            });



                    // Click event for navigation item
                    $switch.click(function() {

                        // Find current visible image
                        var $visible = $("." + settings.imageClass + ":visible", $target);

                        // If the visible image is matching to the clicked one - just leave
                        if ($visible.attr("src") === this.href) {
                            return false;
                        }

                        // Mark the clicked one as active
                        setSwitchToActive($(this));

                        var url = $(this).attr("href");

                        showImage($("." + settings.imageClass + "[src='" +  url + "']", $target));

                        return false;

                    });

                    // Create list item
                    var $listItem = $("<li />").css("opacity", settings.panelOpacity).append($switch);

                    // Append switch to nav bar
                    $switchpanel.append($listItem);
                    $switchpanelContainer.css("z-index", index + 1);

                    // Append list item to nav bar
                    $target.append($item);
                });


                // Chrome's too fast again! :D
                window.setTimeout(function() {

                                showImage($("." + settings.imageClass + ":first", $target));
                                setSwitchToActive($("." + settings.switchpanelClass + " li a:first", $target));

                                startSlideshow();
                                
                            }, 500);
            });

        };

})(jQuery);